package org.zjvis.datascience.service.dag;

import org.zjvis.datascience.common.dto.TaskInstanceDTO;
import org.zjvis.datascience.service.ModelLocalService;

import java.util.concurrent.Callable;

import static org.zjvis.datascience.common.constant.Constant.errorTpl;

public class AlgMoRunner implements Callable<TaskRunnerResult> {

    private ModelLocalService modelLocalService;

    private TaskInstanceDTO instance;

    public AlgMoRunner(ModelLocalService modelLocalService, TaskInstanceDTO instance){
        this.modelLocalService = modelLocalService;
        this.instance = instance;
    }

    @Override
    public TaskRunnerResult call() {
        if (instance.hasPrecautionaryError()) {
            return new TaskRunnerResult(500, String.format(errorTpl, "error happens when init stage."));
        }
        TaskRunnerResult result = null;
        try {
            result = modelLocalService.execRunner(this.instance);
        } catch (Exception e) {
            result = new TaskRunnerResult(500, e.getMessage());
        }
        return result;
    }
}
